
#ifndef OCTAVEARGPARSER_H

#include <gsl_ext/gsl_ext_testfunctions.h>
#include <gsl_ext_cpp/GSLFunctionWrapper.h>

#include <gsl/gsl_multimin.h>
#include <octave/oct.h>
#include <octave/oct-map.h>

class GSLMinimizerWrapper;
class StoppingCriterion;

using namespace std;

/// Implements methods for parsing user-supplied GNU Octave parameters.
/**
 * This class implements methods for generating GSL/GSL++ 
 * function, minimizer and stopping criterion instances from 
 * user-supplied GNU Octave parameters. See the GSL++ manual 
 * for parameter specifications.
 */
class OctaveArgParser
{
 public:
  /// Returns the function type corresponding to the given minimization algorithm name.
  static GSLFunctionWrapper::Type getGSLFunctionType(const string &minimizerName);
  
  /// Generates a GSL/GSL++ function instance.
  /**
   * This method allocates a GSL/GSL++ function instance matching 
   * the given parameters and returns a GSLFunctionWrapper 
   * object that points to the allocated structure.
   *
   * @param name function expression or the name of a predefined test function
   * @param params function parameters, e.g. number of dimensions
   * @param evalParams function, gradient and Hessian evaluation types 
   *                   (symbolic or finite difference)
   * @param type the type of instance to generate (F, FDF or FD2F)
   * @param tfParams the parameter struct into which store the parsed parameters
   * @return a wrapper for the allocated structure 
   *         (gsl_multimin_function, gsl_multimin_function_fdf or gsl_ext_multimin_function_fd2f)
   */
  static GSLFunctionWrapper *getFunctionInstance(const string &name,
                                                 const Octave_map &params,
                                                 const Octave_map &evalParams,
                                                 GSLFunctionWrapper::Type type,
                                                 gsl_ext_testfunction_params *tfParams);

  /// Generates a GSL/GSL++ minimizer instance.
  /**
   * This method allocates a GSL/GSL++ minimizer instance matching 
   * the given parameters and returns a GSLMinimizerWrapper 
   * object that points to the allocated structure.
   *
   * @param a GSL/GSL++ algorithm name
   * @param params algorithm parameters
   * @param linminParams line search parameters
   * @param n the dimension of the minimizer, i.e. the number of variables
   * @param stepSize the parsed initial step length for line searches 
   *                 (if not specified, default value is used)
   * @param tol the parsed line search tolerance, i.e. the 
   *            parameter \f$\mu\f$ (if not specified, default value is used)
   * @return a wrapper for the allocated structure 
   *         (gsl_fminimizer, gsl_fdfminimizer or gsl_ext_fd2fminimizer)
   */
  static GSLMinimizerWrapper *getMinimizerInstance(const string &name,
                                                   const Octave_map &params,
                                                   const Octave_map &linminParams,
                                                   int n,
                                                   double &stepSize,
                                                   double &tol);

  /// Generates a stopping criterion instance.
  /**
   * This method generates a stopping criterion that matches 
   * the given name and parameters.
   */
  static StoppingCriterion *getStoppingCriterionInstance(const string &name, 
                                                         const Octave_map &params);
};

#define OCTAVEARGPARSER_H

#endif
